<?php
namespace app\api\controller;
use app\common\controller\Api;
use app\common\model\option\Log as OptionLog;
use app\common\model\option\Option;
use app\common\model\red\Log;

/**
 * 定时任务接口
 */
class Crontab extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    /**
     * YS释放
     *
     */
    public function option_release()
    {
        $check = Option::whereTime('createtime', 'today')->find();
        if ($check) {
            return '';
        }
        $data = Option::order('id desc')->find();
        if (!$data) {
            $param = [
                'options' => 1000,
                'day' => 1,
                'total' => 1000,
            ];
        } else {
            $option = $data->options + config('site.release');
            $param = [
                'options' => $option,
                'day' =>  $data->day + 1,
                'total' => $data->total + $option,
            ];
        }
        $res = Option::create($param);
        if ($res) {
            $where['invalidtime'] = ['>', time()];
            $where['status'] = ['=', 'normal'];
            $where['state'] = ['=', '1'];
            $logs = Log::all($where);

            //总分红包效率
            $total = Log::where($where)->sum('efficiency');
            foreach ($logs as $v) {
                //个人分红包比例
                $todaypro = bcdiv($v->efficiency, $total, 2);
                //YS分配数量
                $option = bcmul($res->options, $todaypro, 2);
                //每日YS分配
                OptionLog::income($v->user_id, $option, 2, 100, $v->sn, $res->id);
            }
        }
    }
}
